/**
 * 
 */
package com.cmpanyandi.elearning.wordsdriller.activity;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filterable;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.cmpanyandi.elearning.utils.StringUtils;
import com.cmpanyandi.elearning.utils.db.DbAdapter;
import com.cmpanyandi.elearning.utils.db.QueryBuilder;
import com.cmpanyandi.elearning.utils.db.query.LikeCondition.LikeType;
import com.cmpanyandi.elearning.wordsdriller.model.DictionaryItem;

/**
 * Custom cursor list adapter.
 * 
 * @author wojtek
 * 
 */
public class ItemListAdapter extends SimpleCursorAdapter implements Filterable, ListAdapter {

    private static int layout = R.layout.item_row;
    private Long dictionaryId;
    private static String[] from = new String[] { DictionaryItem.NAME, DictionaryItem.CONTEXT };

    // and an array of the fields we want to bind those fields to (in this
    // case just text1)
    private static int[] to = new int[] { R.id.toptext, R.id.bottomtext };

    public ItemListAdapter(Context context, Cursor cursor, Long dictionaryId) {
	super(context, layout, cursor, from, to);
	this.dictionaryId = dictionaryId;

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

	Cursor c = getCursor();

	final LayoutInflater inflater = LayoutInflater.from(context);
	View v = inflater.inflate(layout, parent, false);

	int nameCol = c.getColumnIndex(DictionaryItem.NAME);

	String name = c.getString(nameCol);

	int contextCol = c.getColumnIndex(DictionaryItem.CONTEXT);

	String contextString = c.getString(contextCol);

	/**
	 * Next set the name of the entry.
	 */
	TextView nameText = (TextView) v.findViewById(R.id.toptext);
	if (nameText != null) {
	    nameText.setText(name);
	}

	/**
	 * Next set the name of the entry.
	 */
	TextView contextText = (TextView) v.findViewById(R.id.bottomtext);
	if (contextText != null) {
	    contextText.setText(contextString);
	}

	return v;
    }

    @Override
    public void bindView(View v, Context context, Cursor c) {

	int nameCol = c.getColumnIndex(DictionaryItem.NAME);

	String name = c.getString(nameCol);

	int contextCol = c.getColumnIndex(DictionaryItem.CONTEXT);

	String contextString = c.getString(contextCol);

	/**
	 * Next set the name of the entry.
	 */
	TextView nameText = (TextView) v.findViewById(R.id.toptext);
	if (nameText != null) {
	    nameText.setText(name);
	}

	/**
	 * Next set the name of the entry.
	 */
	TextView contextText = (TextView) v.findViewById(R.id.bottomtext);
	if (contextText != null) {
	    contextText.setText(contextString);
	}
    }

    private Cursor getCursorResult(String constraint) {
	QueryBuilder query = new QueryBuilder();
	query.setSelectClause(DictionaryItem.COLUMNS);
	query.setFromClause(DictionaryItem.TABLE_NAME);
	query.addWhereEqual(DictionaryItem.DICTIONARY, String.valueOf(dictionaryId));
	
	if (StringUtils.isNotEmpty(constraint)) {
	    query.addWhereLike(DictionaryItem.NAME, constraint, LikeType.ARROUD);
	}

	return DbAdapter.getInstance().invokeQuery(query);
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {

	return getCursorResult(StringUtils.getString(constraint));
    }

    @Override
    public String convertToString(Cursor cursor) {
	int nameCol = cursor.getColumnIndexOrThrow(DictionaryItem.NAME);
	return cursor.getString(nameCol);
    }

}
